package com.ztgf.shopping.service.template;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.sys.models.dto.SysPlatformDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.template.ShopActiveRelationMapper;
import com.ztgf.shopping.models.service.template.IShopActiveRelationService;
import com.ztgf.shopping.models.bo.template.ShopActiveRelationSaveBO;
import com.ztgf.shopping.models.dto.template.ShopActiveRelationDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 店铺活动页关联表控制器
 *
 * @author YUE
 * @Date 2020-06-05 13:52:27
 */
@DubboService
public class ShopActiveRelationServiceImpl extends ServiceImpl<ShopActiveRelationMapper, ShopActiveRelationDTO> implements IShopActiveRelationService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private ShopActiveRelationMapper shopActiveRelationMapper;

    @Override
    public boolean add(ShopActiveRelationSaveBO saveBO) {
        ShopActiveRelationDTO dto = new ShopActiveRelationDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 获取默认的店铺模板
     * @return
     */
    @Override
    public ShopActiveRelationDTO getDefaultShopActive(Long shopId){
        Wrapper<ShopActiveRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_default", 0);
        wrapper.eq("shop_id", shopId);
        List<ShopActiveRelationDTO> shopActiveRelationDTOList = shopActiveRelationMapper.selectList(wrapper);
        if(shopActiveRelationDTOList.size() > 0){
            return shopActiveRelationDTOList.get(0);
        }
        return null;
    }

/*    *//**
     * 模板删除后 删除对应的关系
     * @param temId
     * @return
     *//*
    @Override
    public boolean deleteTemRelation(Long temId){
        return sysPlatformMapper.deleteTemRelateionLogIc(temId);
    }*/

    /**
     * 逻辑删除 模板删除后 删除对应的关系
     * @param temId
     * @return
     */
    @Override
    public boolean deleteTemRelationLogic(Long temId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("shop_templet_id", temId).or().eq("activity_templet_id", temId);
        return shopActiveRelationMapper.delete(wrapper) > 0;
    }
}
